const ExcelJs = require('exceljs');
const fs = require('fs');

async function getNo(fileName, trackNo) {
    const workbook = new ExcelJs.Workbook()
    const buffer = fs.readFileSync(fileName);
    await workbook.xlsx.load(buffer);
    const worksheet = workbook.getWorksheet(1);
    worksheet.eachRow((row, index) => {
        const no = row.getCell(5).value;
        if (no && typeof no === 'string' && no.startsWith('FTL')) {
            trackNo.push(`'${no}'`);
        }
    })
    console.log(`${fileName} done`);
}

async function getFile() {
    await fs.readdir(`${__dirname}/in`, async (err, data) => {
        const trackNo = []
        console.log(data);
        for (let fileName of data) {
            await getNo(`${__dirname}/in/${fileName}`, trackNo);
            console.log(trackNo.length);
        }
        await fs.writeFileSync(`${__dirname}/trackNo.txt`, Buffer.from(trackNo.join(',\r\n')));
        console.log(`done`);
    });
}

getFile().then(() => {
    console.log('success');
})

